package LeetCode;

import java.util.ArrayList;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/13.
//
//https://leetcode.cn/problems/valid-sudoku/submissions/531515118/
public class 有效的数独 {
    public static void main(String[] args) {
        char[][] board = {
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}
        };
        System.out.println(isValidSudoku(board));
    }
    public static boolean isValidSudoku(char[][] board) {
        boolean res = true;
        for(int i = 0;i < board.length;i++) {
            ArrayList<Character> tempList = new ArrayList<Character>();
            for(int j = 0;j < board[i].length;j++) {
                if(board[i][j] != '.') {
                    if(tempList.contains(board[i][j])) {
                        return false;
                    }else {
                        tempList.add(board[i][j]);
                    }
                }
            }
        }
        for(int i = 0;i < board[0].length;i++) {
            ArrayList<Character> tempList = new ArrayList<Character>();
            for(int j = 0;j < board.length;j++) {
                if(board[j][i] != '.') {
                    if(tempList.contains(board[j][i])) {
                        return false;
                    }else {
                        tempList.add(board[j][i]);
                    }
                }
            }
        }
        for(int i = 0;i < board[0].length;i+=3) {
            ArrayList<Character> tempList = new ArrayList<Character>();
            for(int j = 0;j < board.length;j++) {
                if(j % 3 == 0)tempList.clear();
                for(int k = 0;k<3;k++) {
                    if(board[j][i + k] != '.') {
                        if(tempList.contains(board[j][i + k])) {
                            return false;
                        }else {
                            tempList.add(board[j][i + k]);
                        }
                    }
                }
            }
        }
        return res;
    }

}
